package com.laomuji.android.mvvm.demo.photo

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.FileProvider
import com.laomuji.android.mvvm.base.BaseActivity
import com.laomuji.android.mvvm.base.BaseActivityViewModel
import com.laomuji.android.mvvm.databinding.ActivityPhotoBinding
import com.laomuji.android.mvvm.utils.ImageUtil
import com.laomuji.android.mvvm.utils.ResourceUtil
import java.io.File
import java.io.IOException

/**
 * 选择图片和拍照的例子
 * @author laomuji666
 * @since 2024/4/25
 */
class PhotoActivity:BaseActivity<ActivityPhotoBinding,BaseActivityViewModel>() {
    private var photoLauncher: ActivityResultLauncher<Intent>? = null
    private var cameraLauncher: ActivityResultLauncher<Intent>? = null
    private var cameraFile: File? = null
    private var cameraUri: Uri? = null


    override fun initView() {
        viewBinding.root.setPadding(0, ResourceUtil.getStatusBarHeight(), 0, 0)
        viewBinding.btnPhoto.setOnClickListener {
            photo()
        }
        viewBinding.btnPostFrom.setOnClickListener {
            camera()
        }
    }

    override fun initData() {
        if (photoLauncher == null) {
            photoLauncher =
                registerForActivityResult(
                    ActivityResultContracts.StartActivityForResult()
                ) {
                    if (it!!.resultCode == RESULT_OK) {
                        showImage(it.data!!.data!!)
                    }
                }
        }

        if (cameraLauncher == null) {
            cameraLauncher =
                registerForActivityResult(
                    ActivityResultContracts.StartActivityForResult()
                ) {
                    if (it!!.resultCode == RESULT_OK) {
                        showImage(cameraUri!!)
                    }
                }
        }
    }

    private fun showImage(uri: Uri){
        val bitmap = ImageUtil.uriToBitmap(uri, contentResolver)
        viewBinding.ivContent.setImageBitmap(bitmap)
    }

    /**
     * 相册选择图片 不需要任何权限
     */
    private fun photo(){
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.setType("image/*")
        photoLauncher!!.launch(intent)
    }

    /**
     * 如果清单中没有写相机权限,调用相机就不需要权限
     * 如果清单中写了相机权限,就必须有权限才可以调用相机
     */
    private fun camera(){
        cameraFile = File(externalCacheDir, "camera.png")
        if (!cameraFile!!.exists()) {
            try {
                cameraFile!!.createNewFile()
            } catch (e: IOException) {
                e.printStackTrace()
                return
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            cameraUri = FileProvider.getUriForFile(this,"com.laomuji.android.mvvm.fileprovider", cameraFile!!)
        } else {
            cameraUri = Uri.fromFile(cameraFile)
        }
        val it = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        it.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri)
        it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        it.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
        cameraLauncher!!.launch(it)
    }
}